package com.learn.tx.service;

import com.learn.tx.dao.EmpDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileNotFoundException;

@Service
public class EmpService {

    @Autowired
    EmpDao empDao;


    // @Transactional(readOnly = true) 增删改操作设置
    // readOnly属性为true会报异常: Queries leading to data modification are not allowed
    public void updateTwice(
            // 修改员工姓名的一组参数
            Integer empId4EditName, String newName,
            // 修改员工工资的一组参数
            Integer empId4EditSalary, Double newSalary
    ) throws FileNotFoundException {
        // 为了测试事务是否生效，执行两个数据库操作，看它们是否会在某一个失败时一起回滚
        empDao.updateEmpSalaryById(empId4EditSalary, newSalary);
        empDao.updateEmpNameById(empId4EditName, newName);
    }

    public String getEmpName(Integer empId) {
        return empDao.selectEmpNameById(empId);
    }

    public void updateEmpName(Integer empId, String empName) throws Exception {
        empDao.updateEmpNameById(empId, empName);
    }


    public void updateEmpNameInner(Integer empId, String empName) {
        empDao.updateEmpNameById(empId, empName);
    }

    public void updateEmpSalaryInner(Integer empId, Double empSalary) {
        empDao.updateEmpSalaryById(empId, empSalary);
    }
}
